package topin.facecat.FaceCat.UI;

import facecat.topin.core.*;
import facecat.topin.grid.*;
import java.util.ArrayList;

public class FCGridDoubleCellEx extends FCGridDoubleCell{
     public FCGridDoubleCellEx()
    {
        setDigit(2);
    }

    public boolean m_underLine;

    public void onPaint(FCPaint paint, FCRect rect, FCRect clipRect, boolean isAlternate)
    {
        int clipW = clipRect.right - clipRect.left;
        int clipH = clipRect.bottom - clipRect.top;
        if (clipW > 0 && clipH > 0)
        {
            if (m_grid != null && m_row != null && m_column != null)
            {
                //判断选中
                String text = getPaintText();
                boolean selected = false;
                if (m_grid.getSelectionMode() == FCGridSelectionMode.SelectCell)
                {
                    ArrayList<FCGridCell> selectedCells = m_grid.getSelectedCells();
                    int selectedCellSize = selectedCells.size();
                    for (int i = 0; i < selectedCellSize; i++)
                    {
                        if (selectedCells.get(i) == this)
                        {
                            selected = true;
                            break;
                        }
                    }
                }
                else if (m_grid.getSelectionMode() == FCGridSelectionMode.SelectFullColumn)
                {
                    ArrayList<FCGridColumn> selectedColumns = m_grid.getSelectedColumns();
                    int selectedColumnsSize = selectedColumns.size();
                    for (int i = 0; i < selectedColumnsSize; i++)
                    {
                        if (selectedColumns.get(i) == m_column)
                        {
                            selected = true;
                            break;
                        }
                    }
                }
                else if (m_grid.getSelectionMode() == FCGridSelectionMode.SelectFullRow)
                {
                    ArrayList<FCGridRow> selectedRows = m_grid.getSelectedRows();
                    int selectedRowsSize = selectedRows.size();
                    for (int i = 0; i < selectedRowsSize; i++)
                    {
                        if (selectedRows.get(i) == m_row)
                        {
                            selected = true;
                            break;
                        }
                    }
                }

                //获取颜色
                FCFont font = null;
                long backColor = FCColor.None;
                long textColor = FCColor.None;
                boolean autoEllipsis = m_grid.autoEllipsis();
                FCGridRowStyle rowStyle = m_grid.getRowStyle();
                if (isAlternate)
                {
                    FCGridRowStyle alternateRowStyle = m_grid.getAlternateRowStyle();
                    if (alternateRowStyle != null)
                    {
                        rowStyle = alternateRowStyle;
                    }
                }
                if (rowStyle != null)
                {
                    if (m_grid.getSelectionMode() != FCGridSelectionMode.SelectFullRow)
                    {
                        if (backColor == FCColor.None)
                        {
                            if (selected)
                            {
                                backColor = rowStyle.getSelectedBackColor();
                            }
                            else if (m_row == m_grid.getHoveredRow())
                            {
                                backColor = rowStyle.getHoveredBackColor();
                            }
                            else
                            {
                                backColor = rowStyle.getBackColor();
                            }
                        }
                    }
                    if (font == null)
                    {
                        font = rowStyle.getFont();
                    }
                    if (textColor == FCColor.None)
                    {
                        if (selected)
                        {
                            textColor = rowStyle.getSelectedTextColor();
                        }
                        else if (m_row == m_grid.getHoveredRow())
                        {
                            textColor = rowStyle.getHoveredTextColor();
                        }
                        else
                        {
                            textColor = rowStyle.getTextColor();
                        }
                    }
                }

                FCHorizontalAlign horizontalAlign = m_column.getCellAlign();
                if (m_style != null)
                {
                    if (m_style.autoEllipsis())
                    {
                        autoEllipsis = m_style.autoEllipsis();
                    }
                    if (m_style.getBackColor() != FCColor.None)
                    {
                        backColor = m_style.getBackColor();
                    }
                    if (m_style.getFont() != null)
                    {
                        font = m_style.getFont();
                    }
                    if (m_style.getTextColor() != FCColor.None)
                    {
                        textColor = m_style.getTextColor();
                    }
                    if (m_style.getAlign() != FCHorizontalAlign.Inherit)
                    {
                        horizontalAlign = m_style.getAlign();
                    }
                }
                FCRect backRect = rect;
                backRect.bottom -= 1;
                paint.fillRect(backColor, backRect);
                FCSize tSize = paint.textSize(text, font, -1);
                FCPoint tPoint = new FCPoint(rect.left + 1, rect.top + clipH / 2 - tSize.cy / 2);
                int width = rect.right - rect.left;
                if (tSize.cx < width)
                {
                    if (horizontalAlign == FCHorizontalAlign.Center)
                    {
                        tPoint.x = rect.left + (rect.right - rect.left - tSize.cx) / 2;
                    }
                    else if (horizontalAlign == FCHorizontalAlign.Right)
                    {
                        tPoint.x = rect.right - tSize.cx - 2;
                    }
                }
                FCRect tRect = new FCRect(tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy);
                if (autoEllipsis && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom))
                {
                    if (tRect.right > clipRect.right)
                    {
                        tRect.right = clipRect.right;
                    }
                    if (tRect.bottom > clipRect.bottom)
                    {
                        tRect.bottom = clipRect.bottom;
                    }
                    paint.drawTextAutoEllipsis(text, textColor, font, tRect);
                }
                else
                {
                    paint.drawText(text, textColor, font, tRect, -1);
                }
            }
        }
    }
}
